package com.example.czy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.czy.base.qo.PageQO;
import com.example.czy.entity.Goods;
import com.example.czy.mapper.GoodsMapper;
import com.example.czy.qo.GoodsQO;
import com.example.czy.service.GoodsService;
import com.example.czy.utils.PageUtil;
import com.example.czy.utils.QueryAlias;import com.example.czy.utils.QueryUtils;
import com.example.czy.vo.GoodsVO;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author czy
 * @Date 2023-07-09
 **/
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    /**
     * 后端自定义条件实例
     *
     * @param page
     * @return
     */
    @Override
    public Page<GoodsVO> pageExample(PageQO page) {
        Map<String, String> param = new HashMap<>();
        // 创建时间在 2024-06-06 00:00:00 到 2024-06-06 23:49:22之间
        param.put("g.createTime", "bt 2024-06-06 00:00:00,2024-06-06 23:49:22");
        // 名称包含 张或李或王
        param.put("name", "lk 张 or 李 or 王");
        // 名称或简介等于 1314
        param.put("name or description", "lk 1314");
        // id等于 1
        param.put("g.id", "1");
        // 排序
        param.put("sort", "createTime desc,status");
        QueryAlias queryAlias = QueryAlias.getInstance("g");
        QueryWrapper queryWrapper = QueryUtils.buildQueryWrapperJson(param, queryAlias);
        return baseMapper.selectByQO(PageUtil.getPage(page), queryWrapper);
    }

    @Override
    public Page<GoodsVO> pageByQO(PageQO page, GoodsQO qo) {
        QueryWrapper queryWrapper = QueryUtils.buildQueryWrapper(qo);
        return baseMapper.selectByQO(PageUtil.getPage(page), queryWrapper);
    }

    @Override
    public Page<GoodsVO> pageByJson(PageQO page, String json) {
        // 设置别名，即前端传过来所有字段默认别名为g
        QueryAlias queryAlias = QueryAlias.getInstance("g");
        QueryWrapper queryWrapper = QueryUtils.buildQueryWrapperJson(json, queryAlias);
        return baseMapper.selectByQO(PageUtil.getPage(page), queryWrapper);
    }

}
